//
//  YZVideoProgressView.m
//  VET
//
//  Created by YZ on 2018/7/16.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZVideoProgressView.h"
//时间间隔
#define kTimeInterval 0.1
//默认时间
#define kDefaultTimeMax 10

@interface YZVideoProgressView()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *upView;

/**
 *  进度值0-1.0之间
 */
@property (nonatomic, assign) CGFloat progressValue;

@property (nonatomic, assign) CGFloat currentTime;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation YZVideoProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.upView];
        
        UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGesAction:)];
        [self.upView addGestureRecognizer:longGes];
        
        self.bgView.layer.cornerRadius = kCurrentDeviceNum(88/2);
        self.upView.layer.cornerRadius = kCurrentDeviceNum(72/2);
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(kCurrentDeviceNum(88), kCurrentDeviceNum(88)));
            make.center.equalTo(self);
        }];
        
        [self.upView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(kCurrentDeviceNum(72), kCurrentDeviceNum(72)));
            make.center.equalTo(self);
        }];
        
        self.timeMax = kDefaultTimeMax;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();//获取上下文
    NSLog(@"width = %f",self.frame.size.width);
    CGPoint center = CGPointMake(kMaxProgressViewWidth/2.0, kMaxProgressViewHeight/2.0);  //设置圆心位置
    CGFloat radius = kMaxProgressViewWidth/2.0-kCurrentDeviceNum(2);  //设置半径
    CGFloat startA = - M_PI_2;  //圆起点位置
    CGFloat endA = -M_PI_2 + M_PI * 2 * _progressValue;  //圆终点位置
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    CGContextSetLineWidth(ctx, kCurrentDeviceNum(4)); //设置线条宽度
    [kColorWithHex(0x32BE60) setStroke]; //设置描边颜色
    CGContextAddPath(ctx, path.CGPath); //把路径添加到上下文
    CGContextStrokePath(ctx);  //渲染
}

- (void)startProgress
{
    self.currentTime += kTimeInterval;
    if (self.timeMax > self.currentTime) {
        self.progressValue = self.currentTime/self.timeMax;
        NSLog(@"progress = %f",self.progressValue);
        [self setNeedsDisplay];
    }
    
    if (self.timeMax <= self.currentTime) {
        [self resetOrignalUI];
        [self clearProgress];
        self.hidden = YES;
        if (_delegate && [_delegate respondsToSelector:@selector(progressStarted)]) {
            [_delegate progressEnded];
        }
    }
}

- (void)resetOrignalUI
{
    self.bgView.layer.cornerRadius = kCurrentDeviceNum(88/2);
    self.upView.layer.cornerRadius = kCurrentDeviceNum(72/2);
    
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kCurrentDeviceNum(88), kCurrentDeviceNum(88)));
    }];
    
    [self.upView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kCurrentDeviceNum(72), kCurrentDeviceNum(72)));
    }];
}

- (void)setProgressUI
{
    self.bgView.layer.cornerRadius = kCurrentDeviceNum(kMaxProgressViewWidth/2);
    self.upView.layer.cornerRadius = kCurrentDeviceNum(60/2);
    
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kMaxProgressViewWidth, kMaxProgressViewHeight));
    }];
    
    [self.upView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kCurrentDeviceNum(60), kCurrentDeviceNum(60)));
    }];
}

- (void)longGesAction:(UILongPressGestureRecognizer *)ges
{
    if (ges.state == UIGestureRecognizerStateBegan) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:kTimeInterval target:self selector:@selector(startProgress) userInfo:nil repeats:YES];
        [self.timer fire];

        [self setProgressUI];

        if (_delegate && [_delegate respondsToSelector:@selector(progressStarted)]) {
            [_delegate progressStarted];
        }
    }else if (ges.state == UIGestureRecognizerStateEnded)
    {
        [self resetOrignalUI];
        [self clearProgress];
        
        if (self.timer) {
            [self.timer invalidate];
            self.timer = nil;
        }
        
        if (_delegate && [_delegate respondsToSelector:@selector(progressStarted)]) {
            [_delegate progressEnded];
        }
    }
}

- (void)clearProgress
{
    self.currentTime = 0;
    self.progressValue = 0;
    [self setNeedsDisplay];
}

#pragma mark -- setters && getters

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.layer.masksToBounds = YES;
        _bgView.backgroundColor = kColorWithHexAlpha(0xFFFFFF, 0.6);
    }
    return _bgView;
}

- (UIView *)upView
{
    if (!_upView) {
        _upView = [UIView new];
        _upView.layer.masksToBounds = YES;
        _upView.backgroundColor = kColorWithHex(0xFFFFFF);
    }
    return _upView;
}

@end
